﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;
using WP.Controls.GridColumn;


using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMSalaryMtn : BaseQryRpt 
    {
        private bool m_bCanTrigerCellValueChangedEven = false;
        private bool[]  mDtlColumnReadOnlyArray ;
        private string mstrCond = "";
        private bool mblnIsLoading = false;
        public frmRMSalaryMtn()
        {
            mblnIsLoading = true;
            InitializeComponent();
            FillTree();
        }


        private enum DtlCoumn
        {
            RowSeqNo=0, PrdYear,PrdMonth, ConfirmSel , WorkShopSeqNo, WorkShopName, WorkerRecID, WorkerID, WorkerName,
            WorkPositionName, WorkDays, WorkDaysSalary, HolidayDays, HolidayDaysSalary,
            /// <summary>
            /// 请假天数
            /// </summary>
            LeaveDays,
            /// <summary>
            /// 总天数
            /// </summary>
            TotalDays,
            /// <summary>
            /// 计件工资
            /// </summary>
            STDSalary,
            WorkSalary,
            HourlySalary,
            /// <summary>
            /// 绩效工资
            /// </summary>
            PRPSalary,
            PrdSalary,RewardAndPenalties,
            OverSpend, AddAndReduce, OverTimeSalary, Subsidy,
            TotSalary_1,EndowmentIns,MedicalIns,UnEmployIns,
            TotSalary_2,PersonTax,WithHoldAndRemitTax,TotSalary,
            Remark, Posted,RecID, ValueChanged, DelButton
        }

        private void tvTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //if (mblnIsLoading) return;
            //if (e.Node.Nodes.Count == 0) //最后一层
            //{
            //    string strMaterialID = "";
            //    strMaterialID = CvtFunc.ToString(e.Node.Text.ToString()).Split('-')[0];
            //    //this.dgList.FindRecord(strMaterialID, "MaterialTypeID");
            //    string strCond = " And WorkShopSeqNo like '" + strMaterialID + "'";
            //    using ( DBAccess dbcon = new DBAccess(false,false ))
            //    {
            //        GetData(dbcon);
            //    }
            //}
        }

        private void InitGridDtl()
        {
            //RowSeqNo, WorkShopSeqNo, WorkShopName, WorkerRecID, WorkerID, WorkerName,            
            this.dgvInputDtl.Columns.Clear();
            this.dgvInputDtl.Rows.Clear();

            DataGridViewTextBoxColumn colRowSeqNo = new DataGridViewTextBoxColumn();
            colRowSeqNo.HeaderText = "序号";
            colRowSeqNo.Visible = true;
            colRowSeqNo.Name = "RowSeqNo";
            colRowSeqNo.Width = 50;
            colRowSeqNo.ReadOnly = true;
            colRowSeqNo.SortMode = DataGridViewColumnSortMode.NotSortable;

            DataGridViewTextBoxColumn colPrdYear = new DataGridViewTextBoxColumn();
            colPrdYear.HeaderText = "年份";
            colPrdYear.Visible = true;
            colPrdYear.Name = "PrdYear";
            colPrdYear.Width = 50;
            colPrdYear.ReadOnly = true;
            colPrdYear.SortMode = DataGridViewColumnSortMode.NotSortable;

            DataGridViewTextBoxColumn colPrdMonth = new DataGridViewTextBoxColumn();
            colPrdMonth.HeaderText = "月";
            colPrdMonth.Visible = true;
            colPrdMonth.Name = "PrdMonth";
            colPrdMonth.Width = 50;
            colPrdMonth.ReadOnly = true;
            colPrdMonth.SortMode = DataGridViewColumnSortMode.NotSortable;

            DataGridViewCheckBoxColumn colSel = new DataGridViewCheckBoxColumn();
            colSel.HeaderText = "审核选择";
            colSel.Visible = true;
            colSel.Name = "Sel";
            colSel.Width = 50;
            colSel.ReadOnly = false ;
            colSel.SortMode = DataGridViewColumnSortMode.NotSortable;

            DataGridViewTextBoxColumn colWorkShopSeqNo = new DataGridViewTextBoxColumn();
            colWorkShopSeqNo.HeaderText = "车间编号";
            colWorkShopSeqNo.Width = 100;
            colWorkShopSeqNo.Name = "WorkShopSeqNo";
            colWorkShopSeqNo.ReadOnly = true;
            colWorkShopSeqNo.Visible = true;

            DataGridViewTextBoxColumn colWorkShopName = new DataGridViewTextBoxColumn();
            colWorkShopName.HeaderText = "车间名称";
            colWorkShopName.Width = 100;
            colWorkShopName.Name = "WorkShopName";
            colWorkShopName.ReadOnly = true;
            colWorkShopName.Visible = true;

            DataGridViewTextBoxColumn colWorkerRecID = new DataGridViewTextBoxColumn();
            colWorkerRecID.HeaderText = "员工代码";
            colWorkerRecID.Width = 100;
            colWorkerRecID.Name = "WorkerRecID";
            colWorkerRecID.ReadOnly = true;
            colWorkerRecID.Visible = false;

            DataGridViewTextBoxColumn colWorkerID = new DataGridViewTextBoxColumn();
            colWorkerID.HeaderText = "员工工号";
            colWorkerID.Width = 100;
            colWorkerID.Name = "WorkerID";
            colWorkerID.ReadOnly = true;
            colWorkerID.Visible = true;

            DataGridViewTextBoxColumn colWorkerName = new DataGridViewTextBoxColumn();
            colWorkerName.HeaderText = "员工姓名";
            colWorkerName.Width = 100;
            colWorkerName.Name = "WorkerName";
            colWorkerName.ReadOnly = true;
            colWorkerName.Visible = true;

            //WorkPositionName,WorkDays,HolidayDays,STDSalary
            DataGridViewTextBoxColumn colWorkPositionName = new DataGridViewTextBoxColumn();
            colWorkPositionName.HeaderText = "岗位";
            colWorkPositionName.Width = 100;
            colWorkPositionName.Name = "WorkPositionName";
            colWorkPositionName.ReadOnly = true;
            colWorkPositionName.Visible = true;

            DataGridViewTextBoxColumn colWorkDays = new DataGridViewTextBoxColumn();
            colWorkDays.HeaderText = "实际出勤天数";
            colWorkDays.Width = 100;
            colWorkDays.Name = "WorkDays";
            colWorkDays.ReadOnly = true;
            colWorkDays.Visible = true;

            DataGridViewTextBoxColumn colWorkDaysSalary = new DataGridViewTextBoxColumn();
            colWorkDaysSalary.HeaderText = "实际出勤工资";
            colWorkDaysSalary.Width = 100;
            colWorkDaysSalary.Name = "WorkDaysSalary";
            colWorkDaysSalary.ReadOnly = true;
            colWorkDaysSalary.Visible = true;

            DataGridViewTextBoxColumn colHolidayDays = new DataGridViewTextBoxColumn();
            colHolidayDays.HeaderText = "带薪休假天数";
            colHolidayDays.Width = 100;
            colHolidayDays.Name = "HolidayDays";
            colHolidayDays.ReadOnly = true;
            colHolidayDays.Visible = true;

            DataGridViewTextBoxColumn colHolidayDaysSalary = new DataGridViewTextBoxColumn();
            colHolidayDaysSalary.HeaderText = "带薪休假工资";
            colHolidayDaysSalary.Width = 100;
            colHolidayDaysSalary.Name = "HolidayDaysSalary";
            colHolidayDaysSalary.ReadOnly = true;
            colHolidayDaysSalary.Visible = true;

             DataGridViewTextBoxColumn colLeaveDays = new DataGridViewTextBoxColumn();
             colLeaveDays.HeaderText = "请假天数";
             colLeaveDays.Width = 100;
             colLeaveDays.Name = "LeaveDays";
             colLeaveDays.ReadOnly = true;
             colLeaveDays.Visible = true;

            DataGridViewTextBoxColumn colTotalDays = new DataGridViewTextBoxColumn();
            colTotalDays.HeaderText = "总天数";
            colTotalDays.Width = 100;
            colTotalDays.Name = "TotalDays";
            colTotalDays.ReadOnly = true;
            colTotalDays.Visible = true;
                

            DataGridViewTextBoxColumn colSTDSalary = new DataGridViewTextBoxColumn();
            colSTDSalary.HeaderText = "固定工资";
            colSTDSalary.Width = 100;
            colSTDSalary.Name = "STDSalary";
            colSTDSalary.ReadOnly = true;
            colSTDSalary.Visible = false;

            //WorkSalary,HourlySalary,PrdSalary,RewardAndPenalties
            DataGridViewTextBoxColumn colWorkSalary = new DataGridViewTextBoxColumn();
            colWorkSalary.HeaderText = "计件工资";
            colWorkSalary.Width = 100;
            colWorkSalary.Name = "WorkSalary";
            colWorkSalary.ReadOnly = true;
            colWorkSalary.Visible = true;

            DataGridViewTextBoxColumn colHourlySalary = new DataGridViewTextBoxColumn();
            colHourlySalary.HeaderText = "计时工资";
            colHourlySalary.Width = 100;
            colHourlySalary.Name = "HourlySalary";
            colHourlySalary.ReadOnly = true;
            colHourlySalary.Visible = true;

            DataGridViewTextBoxColumn colPRPSalary = new DataGridViewTextBoxColumn();
            colPRPSalary.HeaderText = "绩效工资";
            colPRPSalary.Width = 100;
            colPRPSalary.Name = "PRPSalary";
            colPRPSalary.ReadOnly = true;
            colPRPSalary.Visible = true;

            DataGridViewTextBoxColumn colPrdSalary = new DataGridViewTextBoxColumn();
            colPrdSalary.HeaderText = "工龄工资";
            colPrdSalary.Width = 100;
            colPrdSalary.Name = "PrdSalary";
            colPrdSalary.ReadOnly = true;
            colPrdSalary.Visible = true;

            DataGridViewTextBoxColumn colRewardAndPenalties = new DataGridViewTextBoxColumn();
            colRewardAndPenalties.HeaderText = "奖惩";
            colRewardAndPenalties.Width = 100;
            colRewardAndPenalties.Name = "RewardAndPenalties";
            colRewardAndPenalties.ReadOnly = true;
            colRewardAndPenalties.Visible = true;
            colRewardAndPenalties.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colRewardAndPenalties.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            //OverSpend, AddAndReduce, OverTimeSalary, Subsidy
            DataGridViewTextBoxColumn colOverSpend = new DataGridViewTextBoxColumn();
            colOverSpend.HeaderText = "超耗";
            colOverSpend.Width = 100;
            colOverSpend.Name = "OverSpend";
            colOverSpend.ReadOnly = true;
            colOverSpend.Visible = true;
            colOverSpend.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colOverSpend.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            DataGridViewTextBoxColumn colAddAndReduce = new DataGridViewTextBoxColumn();
            colAddAndReduce.HeaderText = "补或扣";
            colAddAndReduce.Width = 100;
            colAddAndReduce.Name = "AddAndReduce";
            colAddAndReduce.ReadOnly = true;
            colAddAndReduce.Visible = true;
            colAddAndReduce.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colAddAndReduce.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            DataGridViewTextBoxColumn colOverTimeSalary = new DataGridViewTextBoxColumn();
            colOverTimeSalary.HeaderText = "加班工资";
            colOverTimeSalary.Width = 100;
            colOverTimeSalary.Name = "OverTimeSalary";
            colOverTimeSalary.ReadOnly = true;
            colOverTimeSalary.Visible = true;
            colOverTimeSalary.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colOverTimeSalary.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            DataGridViewTextBoxColumn colSubsidy = new DataGridViewTextBoxColumn();
            colSubsidy.HeaderText = "补助";
            colSubsidy.Width = 100;
            colSubsidy.Name = "Subsidy";
            colSubsidy.ReadOnly = true;
            colSubsidy.Visible = true;
            colSubsidy.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colSubsidy.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            //TotSalary_1,EndowmentIns,MedicalIns,UnEmployIns
            //应发数1=固定工资+绩效工资+计时工资+工龄工资+奖惩+超耗+补或扣+加班工资+补助 
            DataGridViewTextBoxColumn colTotSalary_1 = new DataGridViewTextBoxColumn();
            colTotSalary_1.HeaderText = "应发数1";
            colTotSalary_1.Width = 100;
            colTotSalary_1.Name = "TotSalary_1";
            colTotSalary_1.ReadOnly = true;
            colTotSalary_1.Visible = true;
            colTotSalary_1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colTotSalary_1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            //养老保险
            DataGridViewTextBoxColumn colEndowmentIns = new DataGridViewTextBoxColumn();
            colEndowmentIns.HeaderText = "养老保险";
            colEndowmentIns.Width = 100;
            colEndowmentIns.Name = "EndowmentIns";
            colEndowmentIns.ReadOnly = true;
            colEndowmentIns.Visible = true;
            colEndowmentIns.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colEndowmentIns.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            //医疗保险
            DataGridViewTextBoxColumn colMedicalIns = new DataGridViewTextBoxColumn();
            colMedicalIns.HeaderText = "医疗保险";
            colMedicalIns.Width = 100;
            colMedicalIns.Name = "MedicalIns";
            colMedicalIns.ReadOnly = true;
            colMedicalIns.Visible = true;
            colMedicalIns.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colMedicalIns.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            //失业保险
            DataGridViewTextBoxColumn colUnEmployIns = new DataGridViewTextBoxColumn();
            colUnEmployIns.HeaderText = "失业保险";
            colUnEmployIns.Width = 100;
            colMedicalIns.Name = "UnEmployIns";
            colUnEmployIns.ReadOnly = true;
            colUnEmployIns.Visible = true;
            colUnEmployIns.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colUnEmployIns.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            //TotSalary_2,PersonTax,WithHoldAndRemitTax,TotSalary
            //应发数2=应发数1-(养老保险基数*保险系数+医疗保险基数*保险系数+失业保险基数*保险系数)
            DataGridViewTextBoxColumn colTotSalary_2 = new DataGridViewTextBoxColumn();
            colTotSalary_2.HeaderText = "应发数2";
            colTotSalary_2.Width = 100;
            colTotSalary_2.Name = "TotSalary_2";
            colTotSalary_2.ReadOnly = true;
            colTotSalary_2.Visible = true;
            colTotSalary_2.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colTotSalary_2.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            
            DataGridViewTextBoxColumn colPersonTax = new DataGridViewTextBoxColumn();
            colPersonTax.HeaderText = "税金";
            colPersonTax.Width = 100;
            colPersonTax.Name = "PersonTax";
            colPersonTax.ReadOnly = true;
            colPersonTax.Visible = true;
            colPersonTax.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colPersonTax.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            DataGridViewTextBoxColumn colWithHoldAndRemitTax = new DataGridViewTextBoxColumn();
            colWithHoldAndRemitTax.HeaderText = "代或缴";
            colWithHoldAndRemitTax.Width = 100;
            colWithHoldAndRemitTax.Name = "WithHoldAndRemitTax";
            colWithHoldAndRemitTax.ReadOnly = true;
            colWithHoldAndRemitTax.Visible = true;
            colWithHoldAndRemitTax.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colWithHoldAndRemitTax.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            
            //实发工资=应发数2-税金-代或缴
            DataGridViewTextBoxColumn colTotSalary= new DataGridViewTextBoxColumn();
            colTotSalary.HeaderText = "实发工资";
            colTotSalary.Width = 100;
            colTotSalary.Name = "TotSalary";
            colTotSalary.ReadOnly = true;
            colTotSalary.Visible = true;
            colTotSalary.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            colTotSalary.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            // Remark, Posted,RecID, ValueChanged, DelButton
            DataGridViewTextBoxColumn colRemark = new DataGridViewTextBoxColumn();
            colRemark.HeaderText = "备注";
            colRemark.Width = 100;
            colRemark.Name = "Remark";
            colRemark.ReadOnly = false;
            colRemark.Visible = true;

            DataGridViewTextBoxColumn colPosted = new DataGridViewTextBoxColumn();
            colPosted.HeaderText = "审核标志";
            colPosted.Width = 100;
            colPosted.Name = "posted";
            colPosted.ReadOnly = true;
            colPosted.Visible = true ;

            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            DataGridViewTextBoxColumn colValueChanged = new DataGridViewTextBoxColumn();
            colValueChanged.HeaderText = "ValueChanged";
            colValueChanged.Visible = false;
            colValueChanged.Name = "ValueChanged";

            DataGridViewImageColumn colDel = new DataGridViewImageColumn();
            Bitmap delbitmap = new Bitmap(this.iglSource.Images["del.ico"]);
            colDel.Width = 20;
            colDel.Name = "DelLine";
            colDel.HeaderText = "";
            colDel.Resizable = DataGridViewTriState.False;
            colDel.Visible = false;
            //RowSeqNo,PrdYear,PrdMonth,WorkShopSeqNo, WorkShopName, WorkerRecID, WorkerID, WorkerName,  
            //WorkPositionName,WorkDays,WorkDaysSalary,HolidayDays,HolidayDaysSalary,STDSalary
            //WorkSalary,HourlySalary,PrdSalary,RewardAndPenalties
            //OverSpend, AddAndReduce, OverTimeSalary, Subsidy
            //TotSalary_1,EndowmentIns,MedicalIns,UnEmployIns
            //TotSalary_2,PersonTax,WithHoldAndRemitTax,TotSalary
            // Remark, Posted,RecID, ValueChanged, DelButton
            this.dgvInputDtl.Columns.AddRange(new DataGridViewColumn[] {  
                colRowSeqNo, colPrdYear, colPrdMonth , colSel ,colWorkShopSeqNo,colWorkShopName, colWorkerRecID, colWorkerID,colWorkerName,
               colWorkPositionName,colWorkDays,colWorkDaysSalary,colHolidayDays, colHolidayDaysSalary,colLeaveDays,colTotalDays,colSTDSalary,
            colWorkSalary,colHourlySalary,colPRPSalary,colPrdSalary,colRewardAndPenalties,
            colOverSpend, colAddAndReduce, colOverTimeSalary, colSubsidy,
            colTotSalary_1,colEndowmentIns,colMedicalIns,colUnEmployIns,
            colTotSalary_2,colPersonTax,colWithHoldAndRemitTax,colTotSalary,
            colRemark, colPosted,colRecID, colValueChanged, colDel
                 });

            mDtlColumnReadOnlyArray = new bool[dgvInputDtl.Columns.Count];
            string[] ColumnWidthArray = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgvInputDtl.Name).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < dgvInputDtl.Columns.Count; i++)
            {
                mDtlColumnReadOnlyArray[i] = dgvInputDtl.Columns[i].ReadOnly;
                if (ColumnWidthArray.Length == dgvInputDtl.Columns.Count) dgvInputDtl.Columns[i].Width = CvtFunc.ToInt(ColumnWidthArray[i]);
            }

            this.dgvInputDtl.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvInputDtl.RowTemplate.Height = 20;
            this.dgvInputDtl.RowHeadersWidth = 23;
            this.dgvInputDtl.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvInputDtl.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvInputDtl.ColumnHeadersHeight = 30;
            this.dgvInputDtl.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvInputDtl.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvInputDtl.AllowUserToResizeRows = false;
        }

        private void FillTree()
        {
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = "select WorkShopSeqNo,WorkShopName from rmWorkShop order by WorkShopSeqNo";
            DataTable dtItem = dbcon.GetDataTable(strSQL);
            foreach (DataRow drType in dtItem.Rows)
            {
                TreeNode tvNode = new TreeNode(CvtFunc.ToString(drType["WorkShopSeqNo"]) + "-" + CvtFunc.ToString(drType["WorkShopName"]));
                this.tvTree.Nodes.Add(tvNode);
            }
            dbcon.CloseCon();
        }

        private void FillDtl(DataRow drSalary,DBAccess dbcon)
        {
            m_bCanTrigerCellValueChangedEven = false;
            if (drSalary != null)
            {
                this.dgvInputDtl.Rows.Add();
                int intCount = this.dgvInputDtl.Rows.Count - 1;
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.AddAndReduce].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["AddAndReduce"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.EndowmentIns].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["EndowmentIns"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.HolidayDays].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["HolidayDays"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.HolidayDaysSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["HolidayDaysSalary"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.HourlySalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["HourlySalary"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.MedicalIns].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["MedicalIns"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.OverSpend].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["OverSpend"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.OverTimeSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["OverTimeSalary"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.PersonTax].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["PersonTax"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.PrdSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["PrdSalary"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.PRPSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["PRPSalary"]), "ns.2");

                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.RecID].Value = CvtFunc.ToLong(drSalary["RecID"]);
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.Posted].Value = CvtFunc.ToBoolean(drSalary["Posted"]) ? "已审核":"" ;

                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.PrdYear].Value = CvtFunc.ToLong(drSalary["PrdYear"]);
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.PrdMonth].Value = CvtFunc.ToLong(drSalary["PrdMonth"]);

                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.Remark].Value = CvtFunc.ToString(drSalary["Remark"]);
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.RewardAndPenalties].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["RewardAndPenalties"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.RowSeqNo].Value = intCount + 1;
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.STDSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["STDSalary"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.Subsidy].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["Subsidy"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.TotSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["TotSalary"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.TotSalary_1].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["TotSalary_1"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.TotSalary_2].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["TotSalary_2"]), "ns.2");

                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.UnEmployIns].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["UnEmployIns"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.WithHoldAndRemitTax].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["WithHoldAndRemitTax"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.WorkDays].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["WorkDays"]), "ns.2");
                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.WorkDaysSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["WorkDaysSalary"]), "ns.2");

                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.WorkSalary].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["WorkSalary"]), "ns.2");

                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.LeaveDays].Value = BaseFunctions.FormatNumeric(CvtFunc.ToDouble(drSalary["LeaveDays"]), "ns.2");
                

                string strSQL = "select * from rmWorker where WorkerID='" + drSalary["WorkerID"] + "'";
                DataRow drWorkerID = dbcon.GetDataFirstRow(strSQL);

                this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.WorkerID].Value = CvtFunc.ToString(drSalary["WorkerID"]);
                if (drWorkerID != null)
                {
                    this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.WorkerName].Value = CvtFunc.ToString(drWorkerID["WorkerName"]);
                    this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.WorkerRecID].Value = CvtFunc.ToLong(drWorkerID["RecID"]);

                    this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.WorkPositionName].Value = CvtFunc.ToString(drWorkerID["WorkPositionName"]);

                    this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.WorkShopSeqNo].Value = CvtFunc.ToString(drWorkerID["WorkShopSeqNo"]);

                    this.dgvInputDtl.Rows[intCount].Cells[(int)DtlCoumn.WorkShopName].Value = CvtFunc.ToString ( dbcon.GetValue("select WorkShopName from rmWorkShop Where WorkShopSeqNo='" + CvtFunc.ToString(drWorkerID["WorkShopSeqNo"]) + "'"));
                    
                }
                CalTotal(dbcon, this.dgvInputDtl.Rows[intCount]);
            }
            m_bCanTrigerCellValueChangedEven = true;
        }

        private void CalTotal(DBAccess dbcon , DataGridViewRow dgvRow)
        {

            dgvRow.Cells[(int)DtlCoumn.TotalDays].Value = CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.WorkDays].Value)
                                                           + CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.HolidayDays].Value)
                                                           + CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.LeaveDays].Value);

            //TotSalary_1,EndowmentIns,MedicalIns,UnEmployIns
            //应发数1=固定工资+绩效工资+计件工资+计时工资+工龄工资+奖惩+超耗+补或扣+加班工资+补助 
            dgvRow.Cells[(int)DtlCoumn.TotSalary_1].Value =
                BaseFunctions.FormatNumeric( 
                  CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.STDSalary].Value) + CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.PRPSalary].Value) +
                  CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.WorkSalary].Value) +
                  CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.HourlySalary].Value) + CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.PrdSalary].Value) +
                  CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.RewardAndPenalties].Value) + CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.OverSpend].Value) +
                  CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.AddAndReduce].Value) + CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.OverTimeSalary].Value) +
                  CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.Subsidy].Value) , "Ns.2");
                        
            //TotSalary_2,PersonTax,WithHoldAndRemitTax,TotSalary
            //应发数2=应发数1-(养老保险基数*保险系数+医疗保险基数*保险系数+失业保险基数*保险系数)
            DataRow drWorker = dbcon.GetDataFirstRow("select * from rmWorker Where WorkerID='" + dgvRow.Cells[(int)DtlCoumn.WorkerID].Value + "'");
            if (drWorker != null)
            {
                dgvRow.Cells[(int)DtlCoumn.UnEmployIns].Value =BaseFunctions.FormatNumeric ( CvtFunc.ToDouble(drWorker["UnEmployIns"])*CvtFunc.ToDouble (drWorker["UnEmployInsRate"]),"Ns.2");
                dgvRow.Cells[(int)DtlCoumn.MedicalIns].Value =BaseFunctions.FormatNumeric ( CvtFunc.ToDouble(drWorker["MedicalIns"])*CvtFunc.ToDouble (drWorker["MedicalInsRate"]),"Ns.2");
                dgvRow.Cells[(int)DtlCoumn.EndowmentIns].Value =BaseFunctions.FormatNumeric ( CvtFunc.ToDouble(drWorker["EndowmentIns"])*CvtFunc.ToDouble (drWorker["EndowmentInsRate"]),"Ns.2");

                dgvRow.Cells[(int)DtlCoumn.TotSalary_2].Value =
                    BaseFunctions.FormatNumeric(CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.TotSalary_1].Value) -
                   (
                        CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.UnEmployIns].Value)+
                        CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.MedicalIns].Value)+
                        CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.EndowmentIns].Value)
                   )
                , "Ns.2");
            }

            //计算税金
            double dblStAmtForTax =3500;
            double dblTotSalary_2 = CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.TotSalary_2].Value);
            double dblAmtOnTax = dblTotSalary_2 - dblStAmtForTax ;
            double dblPersonTax = 0;
            string strSQL = "Select * From mPersonTax Where StRange>='" + dblAmtOnTax + "' and EdRange<='" + dblAmtOnTax + "'";
            DataRow drTax = dbcon.GetDataFirstRow(strSQL);
            if (drTax != null)
            {
                dblPersonTax = dblAmtOnTax * CvtFunc.ToDouble(drTax["TaxPct"]) - CvtFunc.ToDouble(drTax["ReduceAmt"]); 
            }
            else
            {
                dblPersonTax = 0;
            }
            dgvRow.Cells[(int)DtlCoumn.TotSalary_2].Value = BaseFunctions.FormatNumeric(dblTotSalary_2-dblPersonTax, "ns.2");

            //实发工资=应发数2-税金-代或缴
            dgvRow.Cells[(int)DtlCoumn.TotSalary].Value = BaseFunctions.FormatNumeric(
                  CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.TotSalary_2].Value)-
                  CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.PersonTax].Value) -
                  CvtFunc.ToDouble(dgvRow.Cells[(int)DtlCoumn.WithHoldAndRemitTax].Value)
                  , "Ns.2");
        }

        private string GetQryCondition()
        {
            if (tvTree.SelectedNode.Nodes.Count == 0) //车间
            {
                string strMaterialID = "";
                strMaterialID = CvtFunc.ToString(tvTree.SelectedNode.Text.ToString()).Split('-')[0];
                mstrCond = "  '" + strMaterialID + "'";
                return mstrCond;
            }
            else
            {
                return " 1=1 ";
            }
        }

        private void GetData(DBAccess dbcon)
        {
            this.dgvInputDtl.Rows.Clear(); 
            string strSQL = "SELECT Period,StPrdDate FROM MPERIOD where StPrdDate<='{0}' and EnPrdDate>='{1}'";
            string strPrdYear = "", strPrdMonth = "";
            strSQL = string.Format(strSQL, udtStDate.Value , udtEdDate.Value);
            DataTable  dtPrd = dbcon.GetDataTable (strSQL);
            foreach (DataRow drPrd in dtPrd.Rows)
            {
                strPrdYear = CvtFunc.ToDateTime(drPrd["StPrdDate"]).Year.ToString();
                strPrdMonth = CvtFunc.ToString(drPrd["Period"]);
                
                //如果在rmWorkerWorkSalary中没有找到posted=1的情况，就要去其他表中去取。
                //如果找到了，就应该去真正的工资表中去取。
                double dblWorkSalary = 0;
                double dblSTDWorkDays = 0, dblSTDWorkDaysSalary=0, dblHolidayDays = 0, dblHolidayDaysSalary = 0, dblSTDSalary = 0, dblLeaveDays = 0;
                string strQryCondition= " And 1=1 " ;                
                if ( txtWorkShopSeqNo.Text !="") strQryCondition  +=" and workshopseqno in ('" + txtWorkShopSeqNo.Text + "') ";
                if (txtWorkerID.Text !="") strQryCondition +=" and workerid='"+ txtWorkerID.Text +"'";
                if (txtPY.Text !="") strQryCondition +=" and NamePY='"+ txtPY.Text +"'"; 
                strSQL = "select * from rmWorker where 1=1 " + strQryCondition;
                DataTable dtWorker = dbcon.GetDataTable(strSQL);
                foreach (DataRow drWorker in dtWorker.Rows)
                {
                    strSQL = "Select * from rmWorkerSalary Where  WorkerID='{0}' and PrdYear='{1}' and PrdMonth='{2}' and posted=1";
                    DataRow drWorkerSalary = dbcon.GetDataFirstRow( string.Format ( strSQL, drWorker["WorkerID"] , strPrdYear, strPrdMonth));
                    if (drWorkerSalary != null) //如果此员工 已经被审核，则直接填充Grid
                    {
                        FillDtl(drWorkerSalary, dbcon);
                    }
                    else  //如果没有审核，则重新计算
                    {
                        strSQL = "Select top 0 * from rmWorkerSalary Where  WorkerID='{0}' and PrdYear='{1}' and PrdMonth='{2}' and posted=1";
                        drWorkerSalary = dbcon.GetDataTable(strSQL).NewRow();

                        drWorkerSalary["WorkerID"] = drWorker["WorkerID"];
                        drWorkerSalary["PrdYear"] = strPrdYear;
                        drWorkerSalary["PrdMonth"] = strPrdMonth;
                        drWorkerSalary["PrdSalary"] = drWorker["PrdSalary"];

                        strSQL = "Select sum(WorkSalary) as WorkSalary from rmWorkerWorkSalary where WorkerID='{0}' and PrdYear='{1}' and PrdMonth='{2}' ";
                        strSQL = string.Format(strSQL, drWorker["WorkerID"], strPrdYear, strPrdMonth);
                        dblWorkSalary = CvtFunc.ToDouble(dbcon.GetValue(strSQL));
                        drWorkerSalary["WorkSalary"] = dblWorkSalary;

                        strSQL = @"select sum(WorkDays) as WorkDays, sum(WorkDaysSalary) as WorkDaysSalary, sum(HolidayDays) as HolidayDays,sum(HolidayDaysSalary) as HolidayDaysSalary,
                                   sum(LeaveDays) as LeaveDays,sum(WorkSalary) as STDSalary 
                                   from dbo.rmWorkerSTDSalary where WorkerID='{0}' and PrdYear='{1}' and PrdMonth='{2}' and Posted=1 ";
                        strSQL = string.Format(strSQL, drWorker["WorkerID"], strPrdYear, strPrdMonth);
                        DataRow drSTD = dbcon.GetDataFirstRow(strSQL);
                        if (drSTD != null)
                        {
                            dblSTDWorkDays = CvtFunc.ToDouble(drSTD["WorkDays"]);
                            dblSTDWorkDaysSalary = CvtFunc.ToDouble(drSTD["WorkDaysSalary"]);
                            dblHolidayDays = CvtFunc.ToDouble(drSTD["HolidayDays"]);
                            dblHolidayDaysSalary = CvtFunc.ToDouble(drSTD["HolidayDaysSalary"]);
                            
                            dblSTDSalary = CvtFunc.ToDouble(drSTD["STDSalary"]);
                            dblLeaveDays = CvtFunc.ToDouble(drSTD["LeaveDays"]); 
                            
                        }
                        drWorkerSalary["WorkDays"] = dblSTDWorkDays;
                        drWorkerSalary["HolidayDays"] = dblHolidayDays;
                        drWorkerSalary["STDSalary"] = dblSTDSalary;
                        drWorkerSalary["LeaveDays"] = dblLeaveDays;
                        drWorkerSalary["WorkDaysSalary"] = dblSTDWorkDaysSalary;
                        drWorkerSalary["HolidayDaysSalary"] = dblHolidayDaysSalary;

                        strSQL = @"Select sum(RewardAndPenalties) as RewardAndPenalties,                                     
                                      sum(hourlySalary) as hourlySalary,
                                      sum(OverSpend) as OverSpend,
                                      sum(AddAndReduce) as AddAndReduce,
                                      sum(OverTimeSalary) as OverTimeSalary,
                                      sum(Subsidy) as Subsidy,
                                      sum(WithHoldAndRemitTax) as WithHoldAndRemitTax,
                                      sum(PRPSalary) as PRPSalary
                               From rmWorkerOtherSalary 
                               Where WorkerID='{0}' and PrdYear='{1}' and PrdMonth='{2}'";
                        strSQL = string.Format(strSQL, drWorker["WorkerID"], strPrdYear, strPrdMonth);
                        DataRow drOtherSalary = dbcon.GetDataFirstRow(strSQL);
                        if (drOtherSalary != null)
                        {
                            drWorkerSalary["RewardAndPenalties"] = drOtherSalary["RewardAndPenalties"];
                            drWorkerSalary["AddAndReduce"] = drOtherSalary["AddAndReduce"];
                            drWorkerSalary["HourlySalary"] = drOtherSalary["hourlySalary"];
                            drWorkerSalary["OverSpend"] = drOtherSalary["OverSpend"];
                            drWorkerSalary["OverTimeSalary"] = drOtherSalary["OverTimeSalary"];
                            drWorkerSalary["Subsidy"] = drOtherSalary["Subsidy"];
                            drWorkerSalary["WithHoldAndRemitTax"] = drOtherSalary["WithHoldAndRemitTax"];
                            drWorkerSalary["PRPSalary"] = drOtherSalary["PRPSalary"];  
                        }

                        FillDtl(drWorkerSalary, dbcon);
                    }
                }
                base.SetBlankImage(dgvInputDtl);
            }
        }

        private void btnSelAll_Click(object sender, EventArgs e)
        {
            SelAndUnSel(true);
        }

        private void SelAndUnSel( bool blnSel)
        {
            foreach (DataGridViewRow dgvRow in this.dgvInputDtl.Rows)
            {
                //if (!CvtFunc.ToBoolean(dgvRow.Cells[(int)DtlCoumn.Posted].Value))
                {
                    dgvRow.Cells[(int)DtlCoumn.ConfirmSel].Value = blnSel; 
                }
            }
            this.dgvInputDtl.EndEdit();
        }

        private void btnUnSelAll_Click(object sender, EventArgs e)
        {
            SelAndUnSel(false );
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            this.dgvInputDtl.EndEdit();

            using ( DBAccess dbcon = new DBAccess (false,false ))
            {
                try
                {
                    dbcon.BeginTran();
                    RMSalaryDAL rmDAL = new RMSalaryDAL();
                    foreach (DataGridViewRow dgvRow in this.dgvInputDtl.Rows)
                    {
                        if (!CvtFunc.ToBoolean(dgvRow.Cells[(int)DtlCoumn.Posted].Value) && CvtFunc.ToBoolean(dgvRow.Cells[(int)DtlCoumn.ConfirmSel].Value))
                        {
                            DataRow drWorkerSalary = dbcon.GetDataTable("select top 0 * from rmWorkerSalary").NewRow();
                            BaseFunctions.FillDataRowByGridRow(dgvRow, drWorkerSalary);
                            rmDAL.ConfirmSalary(dbcon, drWorkerSalary);
                            dgvRow.Cells[(int)DtlCoumn.RecID].Value = drWorkerSalary["RecID"];
                            dgvRow.Cells[(int)DtlCoumn.Posted].Value = true;
                            dgvRow.Cells[(int)DtlCoumn.ConfirmSel].Value = false;
                            this.dgvInputDtl.EndEdit();
                        }
                    }
                    if (this.dgvInputDtl.Rows.Count > 0)
                    {
                        this.dgvInputDtl.CurrentCell = this.dgvInputDtl.Rows[0].Cells[(int)DtlCoumn.RowSeqNo];
                    }
                    dbcon.CommitTran();
                }
                catch (Exception ex)
                {
                    dbcon.RollbackTran(); 
                }
            }
        }

        protected override void ubtLookUp_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string strCaption = "";
            string strFields = "";
            Control con = sender as Control;

            if (con.Name == ubtWorkerID.Name)
            {
                strSQL = BaseFunctions.GetLookUpSQL("rmWorker");
                strCaption = "员工选择";
                strFields = "WorkerID";
            }
            else if (con.Name == ubtWorkShopSeqNo.Name)
            {
                strSQL = BaseFunctions.GetLookUpSQL("rmWorkShop");
                strCaption = "员工选择";
                strFields = "WorkShopSeqNo";
            }
            else
            {
                base.ubtLookUp_Click(sender, e);
                return;
            }

            frmLookUp frmlookup = new frmLookUp(strSQL, strCaption, con.Text, strFields);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                if (con.Name == ubtWorkerID.Name)
                {
                    txtWorkerID.Text = frmlookup.GetReturnValue("WorkerID");
                }
                else if (con.Name == ubtWorkShopSeqNo.Name )
                {
                    txtWorkShopSeqNo.Text = frmlookup.GetReturnValue("WorkShopSeqNo");
                    lblWorkShopName.Text = frmlookup.GetReturnValue("WorkShopName");
                }
            }
            frmlookup.Close();
        }

        private void Field_Change(object sender, EventArgs e)
        {
            Control con = sender as Control;
            if (con == null) return;
            using (DBAccess dbcon = new DBAccess(false, false))
            {
                if (con.Name == txtWorkerID.Name)
                {
                    GetData(dbcon);
                }
                else if (con.Name == txtPY.Name)
                {
                    GetData(dbcon);
                }
                else if (con.Name == txtWorkShopSeqNo.Name)
                {
                    GetData(dbcon); 
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.dgvInputDtl.EndEdit();

            using (DBAccess dbcon = new DBAccess(false, false))
            {
                try
                {
                    string  intCurPrd = DataFunctions.GetPrdInfo( SystemModules.RM, PeriodRange.PeriodCur , PeriodOption.Period); 
                    if (DataFunctions.CanUnConfirm( dbcon, CvtFunc.ToInt ( intCurPrd)   ))
                    {
                        dbcon.BeginTran();
                        RMSalaryDAL rmDAL = new RMSalaryDAL();
                        foreach (DataGridViewRow dgvRow in this.dgvInputDtl.Rows)
                        {
                            if (CvtFunc.ToBoolean(dgvRow.Cells[(int)DtlCoumn.Posted].Value) && CvtFunc.ToBoolean(dgvRow.Cells[(int)DtlCoumn.ConfirmSel].Value))
                            {
                                string strRecID = CvtFunc.ToString(dgvRow.Cells[(int)DtlCoumn.RecID].Value);
                                if (rmDAL.UnConfirmSalary(dbcon, strRecID))
                                {
                                    dgvRow.Cells[(int)DtlCoumn.RecID].Value = "0";
                                    dgvRow.Cells[(int)DtlCoumn.Posted].Value = false;
                                    dgvRow.Cells[(int)DtlCoumn.ConfirmSel].Value = false;
                                }
                            }
                            else
                            {
                                string strPrdYear = CvtFunc.ToString(dgvRow.Cells[(int)DtlCoumn.PrdYear].Value);
                                string strPrdMonth = CvtFunc.ToString(dgvRow.Cells[(int)DtlCoumn.PrdMonth].Value);
                                string strWorkerID = CvtFunc.ToString(dgvRow.Cells[(int)DtlCoumn.WorkerID].Value);

                                if (rmDAL.UnConfirmSalary(dbcon, strPrdYear, strPrdMonth, strWorkerID))
                                {
                                    dgvRow.Cells[(int)DtlCoumn.RecID].Value = "0";
                                    dgvRow.Cells[(int)DtlCoumn.Posted].Value = false;
                                    dgvRow.Cells[(int)DtlCoumn.ConfirmSel].Value = false;
                                }
                            }
                        }
                        dbcon.CommitTran();
                    }
                }
                catch (Exception ex)
                {
                    dbcon.RollbackTran();
                }
                this.dgvInputDtl.EndEdit();
                if (this.dgvInputDtl.Rows.Count > 0)
                {
                    this.dgvInputDtl.CurrentCell = this.dgvInputDtl.Rows[0].Cells[(int)DtlCoumn.RowSeqNo];
                }
            }
        }

        private void frmRMSalaryMtn_Load(object sender, EventArgs e)
        {
            mblnIsLoading = true;
            InitGridDtl();
            InitControl();
            mblnIsLoading = false;
        }

        private void InitControl()
        {
            udtStDate.Value = DataFunctions.GetPrdInfo(SystemModules.RM, PeriodRange.PeriodCur, PeriodOption.StPrdDate);
            udtEdDate.Value = DataFunctions.GetPrdInfo(SystemModules.RM, PeriodRange.PeriodCur, PeriodOption.EnPrdDate);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmRMSalaryMtn_FormClosing(object sender, FormClosingEventArgs e)
        {
            DataFunctions.SetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.dgvInputDtl);
        }

        private void tvTree_Click(object sender, EventArgs e)
        {
            if (tvTree.Focused)
            {
                if (this.dgvInputDtl.Rows.Count == 0)
                {
                    if (tvTree.SelectedNode != null)
                    {
                        tvTree_AfterSelect(sender, new TreeViewEventArgs(tvTree.SelectedNode));
                    }
                }
            }
        }

        private void btnToExcel_Click(object sender, EventArgs e)
        {
            ToExcel.DataGirdViewToExcel(this.dgvInputDtl);  
        }

        private void btnQry_Click(object sender, EventArgs e)
        {
            using ( DBAccess dbcon = new DBAccess (false,false ))
            {
                GetData(dbcon);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

        }

        private bool UpdateDetail()
        {
            return true;
            RMSalaryDAL rmDAL = new RMSalaryDAL();
            using (DBAccess dbcon = new DBAccess(false, false))
            {
                foreach (DataGridViewRow dgvRow in this.dgvInputDtl.Rows)
                {
                    if (CvtFunc.ToBoolean(dgvRow.Cells[(int)DtlCoumn.ValueChanged].Value))
                    {
                        DataRow drRow =dbcon.GetDataFirstRow( "select * from rmWorkerSalary");
                        //rmDAL.UpdateDetail( dbcon , 
                    }
                }
            }
        }

        private void dgvInputDtl_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (!m_bCanTrigerCellValueChangedEven) return;

            this.dgvInputDtl.Rows[e.RowIndex].Cells[(int)DtlCoumn.ValueChanged].Value = "1";  
        }

    }
}
